//
//  ST_CirclePercentageIntimacyView.swift
//  btvideo
//
//  Created by Jason on 2021/11/17.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
//import Rswift
import RxSwift

class ST_CirclePercentageIntimacyView: UIView {
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        self.addSubview(background_image_v)
        background_image_v.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(76)
        }
        
        
        
        self.addSubview(roundView)
        roundView.snp.makeConstraints { make in
            make.width.height.equalTo(round_view_width)
            make.center.equalTo(background_image_v)
        }
        
        
        self.addSubview(level_image_view)
        level_image_view.snp.makeConstraints { make in
            make.width.height.equalTo(34)
            make.center.equalTo(background_image_v)
        }
        
        self.addSubview(love_image_v)
        love_image_v.snp.makeConstraints { make in
            make.width.height.equalTo(16)
            make.centerX.equalTo(roundView)
            make.centerY.equalTo(roundView.snp.top)
        }
        
        
        addSubview(btn)
        btn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        btn.rx.controlEvent(.touchUpInside).withUnretained(self).subscribe(onNext: { own,_ in
            
            own.parentViewController?.navigationController?.pushViewController(ST_SweetRankViewController(), animated: true)
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var background_image_v:UIImageView = {
        let v = UIImageView(image: ST_ImageUtils.imageName("intimacy_circle_percentage_background_icon"))
        return v
    }()
    
    fileprivate lazy var love_image_v:UIImageView = {
        let v = UIImageView(image: ST_ImageUtils.imageName("intimacy_circle_percentage_love_icon"))
        return v
    }()
    
    fileprivate lazy var level_image_view:UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        return v
    }()
    
    private let round_view_width:CGFloat = 53
    
    fileprivate lazy var roundView:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.clear
        return v
    }()
    
//    Degrees To Radians
    
    fileprivate let start_degrees:CGFloat = -90
    
    fileprivate lazy var start_radians:CGFloat = start_degrees * .pi / 180
    
    fileprivate lazy var circle_shape:CAShapeLayer = {
        // circle shape
        let c = CAShapeLayer()
        
        let circlePath = UIBezierPath(arcCenter: CGPoint (x: round_view_width / 2, y: round_view_width / 2),
                                      radius: round_view_width / 2,
                                      startAngle: start_radians,
                                      endAngle: start_radians,
                                      clockwise: true)
        
        c.path = circlePath.cgPath
        c.strokeColor =  UIColor(red: 1, green: 0.76, blue: 0.03, alpha: 1).cgColor
        c.fillColor = UIColor.clear.cgColor
        c.lineWidth = 3
        return c
    }()
    
    fileprivate lazy var btn:UIButton = {
        let b = UIButton.init(type: UIButton.ButtonType.custom)
        b.backgroundColor = .clear
        return b
    }()
}


extension ST_CirclePercentageIntimacyView{
    
    func bind(progress:CGFloat,url:String) {
        
        level_image_view.pin_setImage(from: url.url)

        
        if circle_shape.superlayer == nil{
            roundView.layer.addSublayer(circle_shape)
        }
         
        let angle = progress * 360 + start_degrees
        
        let radians = angle * .pi / 180
        
        // bezier path
        let circlePath = UIBezierPath(arcCenter: CGPoint (x: round_view_width / 2, y: round_view_width / 2),
                                      radius: round_view_width / 2,
                                      startAngle: start_radians,
                                      endAngle: radians,
                                      clockwise: true)
        
        //: Create the animation from star1 to star2 (infinitely repeat, autoreverse)
        let animation = CABasicAnimation(keyPath: "path")
        animation.toValue = circlePath.cgPath
        animation.duration = 2
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        animation.fillMode = CAMediaTimingFillMode.both
        animation.repeatCount = .greatestFiniteMagnitude // Infinite repeat
        animation.autoreverses = true
        animation.isRemovedOnCompletion = false

        circle_shape.add(animation, forKey: animation.keyPath)
        
    }
    
}
